
#import "DnpToast.h"
#import <MBProgressHUD/MBProgressHUD.h>

@implementation DnpToast

+ (void)show:(NSString *)text {
    if (text.length == 0) return;
    
    UIWindow *window = [self topW];
    if (!window) return;
    
    dispatch_async(dispatch_get_main_queue(), ^{
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:window animated:YES];
        hud.mode = MBProgressHUDModeText;
        hud.label.text = text;
        hud.label.numberOfLines = 0;
        hud.userInteractionEnabled = NO;
        hud.offset = CGPointZero;
        hud.removeFromSuperViewOnHide = YES;
        hud.margin = 9;
        hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
        hud.bezelView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.75];
        hud.contentColor = [UIColor whiteColor];
        
        [hud hideAnimated:YES afterDelay:2.1];
    });
}

+ (UIWindow *)topW {
    if (@available(iOS 13.0, *)) {
        for (UIWindowScene *wsc in [UIApplication sharedApplication].connectedScenes) {
            if (wsc.activationState == UISceneActivationStateForegroundActive) {
                for (UIWindow *win in wsc.windows) {
                    if (win.isKeyWindow) {
                        return win;
                    }
                }
            }
        }
    }
    return [UIApplication sharedApplication].keyWindow;
}

@end
